package entities.profiles;

import entities.User;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Entity
@Table(name = "driver_profile")
@NamedQueries({
    @NamedQuery(name = "DriverProfile.findAll", query = "SELECT d FROM DriverProfile d"),
    @NamedQuery(name = "DriverProfile.findByIdDriverProfile", query = "SELECT d FROM DriverProfile d WHERE d.idDriverProfile = :idDriverProfile"),
    @NamedQuery(name = "DriverProfile.findByEvaluationsTotal", query = "SELECT d FROM DriverProfile d WHERE d.evaluationsTotal = :evaluationsTotal"),
    @NamedQuery(name = "DriverProfile.findByCourtesy", query = "SELECT d FROM DriverProfile d WHERE d.courtesy = :courtesy"),
    @NamedQuery(name = "DriverProfile.findByPunctuality", query = "SELECT d FROM DriverProfile d WHERE d.punctuality = :punctuality"),
    @NamedQuery(name = "DriverProfile.findByDriveCarefully", query = "SELECT d FROM DriverProfile d WHERE d.driveCarefully = :driveCarefully")})
public class DriverProfile implements Serializable {
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "idDriverProfile")
    private User user;
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idDriverProfile")
    private Integer idDriverProfile;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "evaluationsTotal")
    private long evaluationsTotal;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "courtesy")
    private long courtesy;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "punctuality")
    private long punctuality;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "driveCarefully")
    private long driveCarefully;
    
    /*@OneToOne(cascade = CascadeType.ALL, mappedBy = "idDriverProfile")
    private User user;*/

    public DriverProfile() {
        this.evaluationsTotal = 0;
        this.courtesy = 0;
        this.punctuality = 0;
        this.driveCarefully = 0;
    }
    
    /*public DriverProfile(User user) {
        this.user = user;
        this.evaluationsTotal = 0;
        this.courtesy = 0;
        this.punctuality = 0;
        this.driveCarefully = 0;
    }*/

    public Integer getId() {
        return idDriverProfile;
    }

    public void evaluate(int courtesy, int punctuality, int driveCarefully){
        if(courtesy > 10) {
            courtesy = 10;
        }
        if(punctuality > 10) {
            punctuality = 10;
        }
        if(driveCarefully > 10) {
            driveCarefully = 10;
        }
        if(courtesy < 1) {
            courtesy = 1;
        }
        if(punctuality < 1) {
            punctuality = 1;
        }
        if(driveCarefully < 1) {
            driveCarefully = 1;
        }
        ++evaluationsTotal;
        this.courtesy += courtesy;
        this.punctuality += punctuality;
        this.driveCarefully += driveCarefully;
    }
    
    public long getEvaluationsTotal() {
        return evaluationsTotal;
    }

    public long getCourtesy() {
        return courtesy;
    }
    
    public long getPunctuality() {
        return punctuality;
    }
    
    public long getDriveCarefully() {
        return driveCarefully;
    }
    
    public long getCourtesyAverage() {
        if(evaluationsTotal == 0) {
            return 0;
        }
        return courtesy/evaluationsTotal;
    }
    
    public long getPunctualityAverage() {
        if(evaluationsTotal == 0) {
            return 0;
        }
        return punctuality/evaluationsTotal;
    }
    
    public long getDriveCarefullyAverage() {
        if(evaluationsTotal == 0) {
            return 0;
        }
        return driveCarefully/evaluationsTotal;
    }
    
    /*public User getUser() {
        return user;
    }*/

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idDriverProfile != null ? idDriverProfile.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (object == null) {
            return false;
        }
        if (!(object instanceof DriverProfile)) {
            return false;
        }
        DriverProfile other = (DriverProfile) object;
        return !((this.idDriverProfile == null && other.idDriverProfile != null) || (this.idDriverProfile != null && !this.idDriverProfile.equals(other.idDriverProfile)));
    }

    @Override
    public String toString() {
        return "entities.profiles.DriverProfile[ idDriverProfile=" + idDriverProfile + " ]";
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}
